package com.jia.jianzhiOffer;

import java.util.HashMap;

/**
 * @program: Leetcode
 * @description:
 *    无重复字符的最长子串
 * @author: STU756
 * @create: 2020-08-05 01:22
 */
public class Pro48_1 {
    public int lengthOfLongestSubstring(String s) {
        int len;
        if(s == null || (len = s.length()) == 0) { return 0; }
        HashMap<Character, Integer> map = new HashMap<>();
        int pre = 0;
        int max = 0;
        for(int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if(map.containsKey(c)) {
                max = Math.max(i - pre, max);
                pre = Math.max(map.get(c) + 1, pre);
            }
            map.put(c, i);
        }
        return Math.max(max, len - pre);
    }
}
